package com.zebraphp.devtool.httpUtils.NetOkHttp.responseHandler;

import com.zebraphp.devtool.httpUtils.NetOkHttp.IOkHttp;

import java.io.IOException;

import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * raw 字符串结果回调
 */
public abstract class RawParseHandler implements BaseHandler {

    @Override
    public final void onOk(final Response response) {
        ResponseBody responseBody = response.body();
        String responseBodyStr = "";

        try {
            responseBodyStr = responseBody.string();

        } catch (IOException e) {
            IOkHttp.mHandlerQueue.post(new Runnable() {
                @Override
                public void run() {
                    onError(response.code(), "fail read response body");
                }
            });
            return;

        } finally {
            responseBody.close();
        }

        final String finalResponseBodyStr = responseBodyStr;
        IOkHttp.mHandlerQueue.post(new Runnable() {
            @Override
            public void run() {
                onOkParse(response.code(), finalResponseBodyStr);
            }
        });

    }

    /*对请求结果 进行 解析*/
    public abstract void onOkParse(int statusCode, String response);

    @Override
    public void onProgress(long currentBytes, long totalBytes) {

    }
}
